package cn.iocoder.yudao.module.biz.controller.admin.contract.expense;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.contract.expense.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.contract.expense.ContractArchiveExpenseDO;
import cn.iocoder.yudao.module.biz.service.contract.expense.ContractArchiveExpenseService;

@Tag(name = "管理后台 - 合同档案-支出")
@RestController
@RequestMapping("/san/biz-contract-archive-expense")
@Validated
public class ContractArchiveExpenseController {

    @Resource
    private ContractArchiveExpenseService contractArchiveExpenseService;

    @PostMapping("/create")
    @Operation(summary = "创建合同档案-支出")
    @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:expense')")
    public CommonResult<Long> createContractArchiveExpense(@Valid @RequestBody ContractArchiveExpenseSaveReqVO createReqVO) {
        return success(contractArchiveExpenseService.createContractArchiveExpense(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合同档案-支出")
    @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:expense')")
    public CommonResult<Boolean> updateContractArchiveExpense(@Valid @RequestBody ContractArchiveExpenseSaveReqVO updateReqVO) {
        contractArchiveExpenseService.updateContractArchiveExpense(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合同档案-支出")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:expense')")
    public CommonResult<Boolean> deleteContractArchiveExpense(@RequestParam("id") Long id) {
        contractArchiveExpenseService.deleteContractArchiveExpense(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除合同档案-支出")
                @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:expense')")
    public CommonResult<Boolean> deleteContractArchiveExpenseList(@RequestParam("ids") List<Long> ids) {
        contractArchiveExpenseService.deleteContractArchiveExpenseListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同档案-支出")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:expense')")
    public CommonResult<ContractArchiveExpenseRespVO> getContractArchiveExpense(@RequestParam("id") Long id) {
        ContractArchiveExpenseDO contractArchiveExpense = contractArchiveExpenseService.getContractArchiveExpense(id);
        return success(BeanUtils.toBean(contractArchiveExpense, ContractArchiveExpenseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合同档案-支出分页")
    @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:expense')")
    public CommonResult<PageResult<ContractArchiveExpenseRespVO>> getContractArchiveExpensePage(@Valid ContractArchiveExpensePageReqVO pageReqVO) {
        PageResult<ContractArchiveExpenseDO> pageResult = contractArchiveExpenseService.getContractArchiveExpensePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContractArchiveExpenseRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合同档案-支出 Excel")
    @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:expense')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportContractArchiveExpenseExcel(@Valid ContractArchiveExpensePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContractArchiveExpenseDO> list = contractArchiveExpenseService.getContractArchiveExpensePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "合同档案-支出.xls", "数据", ContractArchiveExpenseRespVO.class,
                        BeanUtils.toBean(list, ContractArchiveExpenseRespVO.class));
    }

}